package com.yys.apm.logger;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Yingsheng Ye
 * @date 2021-03-22 17:09
 */
public abstract class AbstractLogger {

    private final static String PATTERN = "%s  %s  %s --- [%s] %s  : %s";

    void printLog(LogLevel logLevel, String msg, Throwable throwable) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String date = dateFormat.format(new Date());
        String info = String.format(PATTERN, date, logLevel.name(), Thread.currentThread().getId(),
                Thread.currentThread().getName(), getClassName(), msg);
        if (logLevel.name().equals(LogLevel.ERROR.name())) {
            System.err.println(info);
        } else {
            System.out.println(info);
        }
        if (null != throwable) {
            throwable.printStackTrace();
        }
    }

    protected abstract String getClassName();

}
